/* 
Copyright 2008 Cheezeworld.com 

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
package ObjectManagement
{
	import com.cheezeworld.entity.Entity;
	import com.cheezeworld.entity.EntityParams;
	import com.cheezeworld.entity.GameWorld;
	import com.cheezeworld.entity.MovingEntity;
	import com.cheezeworld.entity.MovingEntityParams;
	import com.cheezeworld.math.Vector2D;
	import com.cheezeworld.rendering.BitmapRenderer;
	import com.cheezeworld.rendering.GameWorldRenderer;
	import com.cheezeworld.rendering.SpriteRenderer;
	import com.cheezeworld.screens.AScreen;
	import com.cheezeworld.screens.IScreenItem;
	import com.cheezeworld.utils.Input;
	import com.cheezeworld.utils.Utils;
	
	import flash.display.Sprite;
	import flash.text.TextField;

	public class GameWorldDemo extends AScreen
	{
		private const MAX_ENTITIES:int	=	50;
		
		[Embed( source="../assets/citybg.png" )]
		public var BuildingsBitmap:Class;
		
		[Embed( source="../assets/dog.png" )]
		public var DogBitmap:Class;
		
		public function GameWorldDemo(a_parentScreen:IScreenItem=null)
		{
			super(a_parentScreen);
			
			// Initialize the gameworld and it's renderer
			m_gameWorld = new GameWorld( new Vector2D( 1024, 768 ), new Vector2D( 550, 400 ) );
			m_gwRenderer = new GameWorldRenderer( m_gameWorld, this );
			
			// Create a BG graphic
			var bgParams:EntityParams = new EntityParams();
			bgParams.id = "background";
			bgParams.layer = 1;
			bgParams.scale = 1;
			bgParams.parent = m_gameWorld;
			var bg:Entity = new Entity( bgParams );
			bg.bounds.setSize( 1024, 768 );
			bg.newPos.Set( 1024/2, 768/2 );
			new BitmapRenderer( bg, m_gameWorld.camera, m_gwRenderer.getLayer( bg.layer ), new BuildingsBitmap().bitmapData );
			
			// Spawn some entities
			var entity:MovingEntity;
			var params:MovingEntityParams = new MovingEntityParams();
			params.layer = 3;
			params.scale = 1;
			params.parent = m_gameWorld;
			params.radius = 10;
			params.boundsBehavior = MovingEntity.BOUNDS_BOUNCE;
			params.maxSpeed = 20;
			params.friction = .9;
			m_gravity = new Vector2D( 0, 10 );
			for( var i:int = 0; i < MAX_ENTITIES; i++ )
			{
				var rx:int = Math.random() * 1024;
				var ry:int = Math.random() * 200;
				var sprite:Sprite = new Sprite();
				sprite.addChild( new DogBitmap() );
				entity = new MovingEntity( params );
				entity.newPos.Set( rx, ry );
				entity.addConstantForce( m_gravity, "gravity" );
				new SpriteRenderer( entity, m_gameWorld.camera, m_gwRenderer.getLayer( entity.layer ), sprite );
			}
			entity.scale = 1.5;	
			
			// Tell the camera to follow the last entity created.
			m_gameWorld.camera.followPos( entity.actualPos );
			
			// Instructions! :P
			m_tf = Utils.createTextField( this, 50, 350 );
			m_tf.htmlText = Utils.createHtmlText( "Hold mouse to make dogs attract to mouse position.", "#00FF00", 16 );
		}
		
		public override function update(a_timePassed:int):void
		{
			m_gameWorld.update( a_timePassed );
			
			// When the mouse is held down all entities will accelerate towards it. 
			if( Input.instance.isMouseDown )
			{
				var entities:Array = m_gameWorld.getChildren();
				for each( var entity:Entity in entities )
				{
					if( entity is MovingEntity )
					{
						var movEnt:MovingEntity = entity as MovingEntity;
						var vectorToTarget:Vector2D = Input.instance.worldMousePos.subtractedBy( movEnt.actualPos );
						var dist:Number = movEnt.actualPos.distanceTo( Input.instance.worldMousePos );
						vectorToTarget.normalize();
						vectorToTarget.multiply( movEnt.maxSpeed <= dist ? ( movEnt.maxSpeed ) : ( dist ) ); 
						movEnt.applyForce( vectorToTarget );
					}
				}
			}
		}
		
		private var m_tf:TextField;
		private var m_gameWorld:GameWorld;
		private var m_gwRenderer:GameWorldRenderer;
		private var m_gravity:Vector2D;
	}
}